package com.chenjun.web.controller.activiti;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModelUnPaginationInterceptor;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/process/definition")
public class DefinitionController {
	
	@Resource
	private RepositoryService repositoryService;
	
	/**
	 * 页面展示
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("activiti/process/definition/list");
		return mv;
	}
	
	/**
	 * 分页查询流程
	 * @param datagrid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/search")
	public void getDeployPage(DataGridModelUnPaginationInterceptor datagrid, HttpServletResponse response) throws Exception{
		int rows = datagrid.getRows();
		int page = datagrid.getPage();
		int start = datagrid.getStart();
		Map<String, Object> params = datagrid.getParams();
		String processName = params.isEmpty() ? "" : params.get("processName").toString();
		
		ResultDto rtDto = new ResultDto();
		
		//当前页
		int pageNo = start / rows;
		long count=repositoryService.createProcessDefinitionQuery()
				.processDefinitionNameLike("%"+processName+"%")
				.count();
		List<ProcessDefinition> processDefinitionList=repositoryService.createProcessDefinitionQuery()
				.orderByDeploymentId().desc()
				.processDefinitionNameLike("%"+processName+"%")
				.listPage(pageNo, rows);
		
		Pagination data = new Pagination();
		data.setAaData(processDefinitionList);
		data.setiTotalDisplayRecords((int)count);
		data.setiTotalLongRecords(count);
		data.setiTotalRecords((int)count);
		data.setsEcho(page);
		rtDto.setData(data);
		
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.setExcludes(new String[]{"identityLinks","processDefinition"});
		JSONObject result= JSONObject.fromObject(rtDto, jsonConfig);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();
	}
	
	/**
	 * 查看流程图
	 * @param deploymentId  流程ID
	 * @param diagramResourceName
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showView")
	public String showView(String deploymentId,String diagramResourceName,HttpServletResponse response)throws Exception{
		InputStream inputStream=repositoryService.getResourceAsStream(deploymentId, diagramResourceName);
		OutputStream out=response.getOutputStream();
		for(int b=-1;(b=inputStream.read())!=-1;){
			out.write(b);
		}
		out.close();
		inputStream.close();
		return null;
	}

}
